<?php
namespace app\api\controller\kuang;
use app\api\controller\Common;
use think\Db;
class Shequ extends Common{	//个人中心 需验证登陆
	public function _empty($name){
        die('非法访问！你的IP已被监控！');
    }
    public function jiagou(){   //矿场架构
        $appdata = $this->appdata;      //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;      //系统参数
        // apifasong($appdata);

        //传来数据合法性验证
        $this_u = userinfobyid($appdata['uid']);
        if(!$this_u){
            apifasong(['zt'=>'0','msg'=>'该用户不存在 或已被删除']);
        }
        if($this_u['id']<>$userinfo['id'] && chickusposition($userinfo,$this_u)<>'shang'){
            apifasong(['zt'=>'0','msg'=>'不能查看伞外用户']);
        }
        //非顶级用户 只能查看2代会员
        if($userinfo['id']<>1 && $this_u['id']<>$userinfo['id']){ //不是顶级账号 不是自己
            if($this_u['tid']<>$userinfo['id']){    //1代
                $this_fu = userinfobyid($this_u['tid']);  //this_u的上级
                if($this_fu['tid']<>$userinfo['id']){    //2代
                    $this_fu = userinfobyid($this_u['tid']);  //this_u的上级
                    if($this_fu['tid']<>$userinfo['id']){
                        apifasong(['zt'=>'0','msg'=>'只能查看3代会员']);
                    }
                }
            }
        }
        //获取下级列表
        $lists=db('user')->where('tid',$this_u['id'])
            ->column('id,uname,tid,nicename,zt,level,issq,regtime,headimg,power,is_effect');
        if($lists){
            $id = $this_u['id'];
            //伞下ID集合（不含自己）
            $ids_sx = db('user')->where("FIND_IN_SET({$id},tpath)")->column('id');
            //获取伞下人数（含自己）
            $num['sanxia'] = count($ids_sx) + 1;
            //获取直推人数
            $num['zhitui']=count($lists);
            //伞下算力
            // $num['sum_suanli']=db('user')->where('uid','in',$ids_sx)->where('status',1)->sum('power');
            $num['sum_suanli'] = db('user')->where("FIND_IN_SET({$id},tpath)")->sum('power');
            $num['sum_suanli'] += $userinfo['power'];
            apifasong(['zt'=>'1','lists'=>$lists,'num'=>$num]);
        }else{
            apifasong(['zt'=>'0','msg'=>'这里没有人']);
        }
    }
    public function shenqing_base(){ //申请社区_页面基础信息
        $appdata = $this->appdata;      //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;      //系统参数
        // apifasong(['zt'=>'1','msg'=>$appdata]);
        $shequ = db('shequ')->where('uid',$userinfo['id'])->find();
        if(!$shequ){
            $shequ='';
        }
        apifasong(['zt'=>'1','userinfo'=>apiuserbaseinfo($userinfo),'shequ'=>$shequ]);
    }
    public function shenqing(){ //申请社区/重新申请_提交
        $appdata = $this->appdata;      //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;      //系统参数
        // apifasong(['zt'=>'1','msg'=>$appdata]);
        //必填项
        if(!$appdata['name']){
            apifasong(['zt'=>'0','msg'=>'请输入社区名称']);
        }
        if(!$appdata['xuanyan']){
            apifasong(['zt'=>'0','msg'=>'请输入申请理由']);
        }
        //合法性
        // if($userinfo['lv']<3){
        //     apifasong(['zt'=>'0','msg'=>'OM'.$userinfo['lv'].'级别还不能申请']);
        // }
        $shequ=db('shequ')->where('uid',$userinfo['id'])->find();
        if($shequ && $shequ['zt']==0){
            apifasong(['zt'=>'0','msg'=>'你已提交申请 请耐心等待系统审核']);
        }
        if($shequ && $shequ['zt']==1){
            apifasong(['zt'=>'0','msg'=>'你已有社区']);
        }
        //写入数据
        if(!$shequ){
            $shequid=db('shequ')->insertGetId([
                'uid'=>$userinfo['id'],
                'uname'=>$userinfo['uname'],
                'name'=>$appdata['name'],
                'xuanyan'=>$appdata['xuanyan'],
                'ctime'=>time(),
                'zt'=>0,
            ]);
            $shequ=db('shequ')->where('id',$shequid)->find();
        }elseif($shequ['zt']==2){   //驳回重新提交的
            $shequid=db('shequ')->where('uid',$userinfo['id'])->update([
                // 'uid'=>$userinfo['id'],
                // 'uname'=>$userinfo['uname'],
                'name'=>$appdata['name'],
                'xuanyan'=>$appdata['xuanyan'],
                'ctime'=>time(),
                'zt'=>0,
            ]);
            $shequ=db('shequ')->where('uid',$userinfo['id'])->find();
        }
        if($shequid){
            apifasong(['zt'=>'1','msg'=>'提交成功 请耐心等待系统审核','shequ'=>$shequ]);
        }else{
            apifasong(['zt'=>'0','msg'=>'申请失败']);
        }
    }
    public function my_base(){ //我的社区 基本信息
        $appdata = $this->appdata;      //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;      //系统参数
        $shequ = array();
        // apifasong(['zt'=>'1','msg'=>$appdata]);
        $shequ_uids=db('shequ')->where('zt',1)->column('uid');  //所有社长的uids
        if($userinfo['issq']){  //自己是社长
            $where['id']=$userinfo['issq'];
            $where['uid']=$userinfo['id'];
            $where['zt']=1;
            $shequ = db('shequ')->where($where)->find();
        }else{  //不是社长
            //寻找最近社区
            $tpath=explode(",",$userinfo['tpath']);
            $tpath=array_reverse($tpath);   //反序
            foreach ($tpath as $val) {
                if(in_array($val,$shequ_uids)){
                    $shequ = db('shequ')->where('uid',$val)->find();
                    break;
                }
            }
        }
        if($shequ){
            $shezhang = userinfobyid($shequ['uid']);
            $shequ['shezhang'] = ($shezhang['nicename']?:$shezhang['truename'])?:$shezhang['uname'];
            $shequchengyuan=shequchengyuan($shequ['uid'],$shequ_uids);  //含会长
            $shequ['renshu'] = count($shequchengyuan);  //含会长
            apifasong(['zt'=>'1','shequ'=>$shequ,'shequchengyuan'=>$shequchengyuan]);
        }else{
            apifasong(['zt'=>'0','msg'=>'没有找到所属公会']);
        }

    }

    public function my_xiugai(){    //我的社区信息修改
        $appdata = $this->appdata;      //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;      //系统参数

        //必填项
        if(!input('name')){
            apifasong(['zt'=>'0','msg'=>'请输入社区名称']);
        }
        if(!input('xuanyan')){
            apifasong(['zt'=>'0','msg'=>'请输入申请理由']);
        }

        //合法性
        $shequ=db('shequ')->where(['id'=>$userinfo['issq'],'uid'=>$userinfo['id'],'zt'=>1])->find();
        // if($userinfo['lv']<3 || !$userinfo['issq'] || !$shequ){
        if(!$shequ){
            apifasong(['zt'=>'0','msg'=>'非法操作']);
        }

        //写入数据
        $ret=db('shequ')->where('id',$shequ['id'])->update([
            'name'=>$appdata['name'],
            'xuanyan'=>$appdata['xuanyan'],
            'etime'=>time()
        ]);
        if($ret){
            apifasong(['zt'=>'1','msg'=>'修改成功']);
        }else{
            apifasong(['zt'=>'0','msg'=>'修改失败']);
        }
    }
}






